(function(factory) {
    var L;
    if (typeof define === 'function' && define.amd) {
        // AMD
        define(['leaflet'], factory);
    } else if (typeof module === 'object' && typeof module.exports === "object") {
        // Node/CommonJS
        L = require('leaflet');
        module.exports = factory(L);
    } else {
        // Browser globals
        if (typeof window.L === 'undefined')
            throw 'Leaflet must be loaded first';
        factory(window.L);
    }
})(function(L) {

    /**
     * 宝船网海图(外网访问被禁，暂不能用)
     * http://enc-cmap2.myships.com/ChartMap/L02/R00000001/C00000003.png
     */
    L.TileLayer.ChartLayer = L.TileLayer.extend({

        initialize: function(url, options) {
            L.TileLayer.prototype.initialize.call(this, url, options);
            this._url = url + "{s}/{x}.png";
        },

        getTileUrl: function(tilePoint) {
            return L.Util.template(this._url, L.extend({
                s: function() {
                    var zl = tilePoint.z;
                    var ty = tilePoint.y;
                    var dir = levDir = rowDir = "";
                    if (zl < 2) {
                        levDir = "LN" + (zl).toString();
                    } else if (zl < 12) {
                        levDir = "L0" + (zl - 2).toString();
                    } else {
                        levDir = "L" + (zl - 2).toString();
                    }
                    rowDir = "R" + this.getHexString(ty);
                    dir = levDir + "/" + rowDir;
                    return dir;
                }.bind(this),
                x: "C" + this.getHexString(tilePoint.x)
            }));
        },

        getHexString: function(value) {
            var strHex = value.toString(16);
            switch (strHex.length) {
                case 1:
                    strHex = "0000000" + strHex;
                    break;
                case 2:
                    return strHex = "000000" + strHex;
                    break;
                case 3:
                    strHex = "00000" + strHex;
                    break;
                case 4:
                    strHex = "0000" + strHex;
                    break;
                case 5:
                    strHex = "000" + strHex;
                    break;
                case 6:
                    strHex = "00" + strHex;
                    break;
                case 7:
                    strHex = "0" + strHex;
                    break;
                default:
                    ;
            }
            return strHex;
        }
    });

    L.tileLayer.ChartLayer = function(url, options) {
        return new L.TileLayer.ChartLayer(url, options);
    };

    /*
     * 船讯网海图
     * http://m2.shipxy.com/tile.c?l=Na&m=o&z=8&y=116&x=204
     */
    L.TileLayer.ChartLayer2 = L.TileLayer.extend({

        getTileUrl: function(tilePoint) {
            return L.Util.template(this._url, L.extend({
                x: tilePoint.x,
                y: tilePoint.y,
                z: tilePoint.z
            }));
        }

    });

    L.tileLayer.ChartLayer2 = function(url, options) {
        return new L.TileLayer.ChartLayer2(url, options);
    };

});
